package cn.tedu.v6;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 *V4  客户端【登录】    退出：退2次
 */
public class Client {
    //优化②：动态数据全部私有化属性到类上----方便修改
    private static final String HOST ="192.168.100.100";
    private static final Integer PORT = 191;
    private Socket socket;
    private String ip;
    //1.置空流   升级   JavaOOP  封装---私有化属性：本类任意地方都可以使用
    private PrintWriter pw;
    private BufferedReader br;
    private String serverMessage;
    public Client() {
        try {
            //私有化socket---拨通服务器电话---返回socket【不能省】
            socket = new Socket(HOST, PORT);
            ip = socket.getInetAddress().getHostAddress();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
            String ip = socket.getInetAddress().getHostAddress();
            System.out.println("服务端" + ip + "连接上了");
        } catch (SocketException e) {
            System.out.println("链接被断开或者重置");
        } catch (UnknownHostException e) {//用户地址出问题异常
            System.out.println("IP地址有问题，请询问服务器端");
        } catch (IOException e) {//IO异常：连接失效，客户端主动退出了
            System.out.println("客户端主动退出了：断开连接");
        }
    }
    public void start() {
        System.out.println("~~~♥welcome to “耶耶” 牌聊天室♥~~~");
        Scanner input=new Scanner(System.in);
        try {
            //一.接收登录提示
            //1.读入服务器端---输出流
            serverMessage = br.readLine();  //服务器端：写“请输入用户名”
            System.out.println(serverMessage);//读入账号，必须写出
            //2.写出用户名
            String username = input.nextLine();//客户端：输入用户名
            //3.写出用户名：发给服务器
            pw.println(username);//写出给服务器端-----获取账号

            //二、接收登录的密码提示
            serverMessage = br.readLine();//服务器端：写出“请输入密码：”
            System.out.println(serverMessage);

            // 三、服务器端验证账号密码是否正确
            //3.1读入账号密码正确情况
            serverMessage = br.readLine();//登陆成功
            System.out.println(serverMessage);
            String password= input.nextLine();
            pw.println(password);
            //3.2判断如果是“登陆成功”，前缀：登陆成功
            if (serverMessage.startsWith("登陆成功")) {
                chat();
            }
        } catch (IOException e) {
            System.out.println("客户端断开链接");
        } finally {
            if (pw!=null)
                pw.close();
            if (br!=null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败~~");
                }
            if(socket!=null&&!socket.isClosed()) {
                System.out.println(ip + "客户端关闭成功！！！success！");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println(ip + "客户端关闭失败了~~");
                }
            }

        }

    }



    //1.start   方法下--封装    客户端通话方法  chat
    private void chat() {
        Scanner input=new Scanner(System.in);
        new Thread(() -> {
            System.out.println("客户端请输入：");
            try {
                String message;
                while ((message =br.readLine()) != null) {
                    System.out.println(ip+"服务器端端说："+message);

                    if (message.equalsIgnoreCase("exit")) {
                        System.exit(0);
                        System.out.println("退出聊天室~~");
                    }
                }
            } catch (IOException e) {
                System.out.println("服务器端端发来的消息断开了~");
            }
        }).start();

        while (true) {
            String say = input.nextLine();
            pw.println(ip + "客户端说" + say);
            System.out.println("请输入聊天内容（输入exit退出）");
            if (say.equalsIgnoreCase("exit")) {
                System.out.println("退出啦~~~");
                System.exit(0);

            }
            pw.println("客户端说："+say);
        }

    }


    public static void main(String[] args) {
        new cn.tedu.v5.Client().start();

    }
}
